#define PLUGIN_EXPORTS
#include "LinkdoodBridge.h"
#include "bridgeHandle.h"

#include <iostream>
#include <mutex>

static std::mutex s_uuidMutex;

PLUGIN_API int initBridge(PHandle* pHandle, const char * initData)
{
	bridgeHandle* pCurrentHandle = new bridgeHandle;
	pCurrentHandle->Init(initData);
	*pHandle = pCurrentHandle; 
	return 0;
}

PLUGIN_API const char* getUuid()
{
	static std::string struuid = "";
	{
		std::lock_guard<std::mutex> lock(s_uuidMutex);	
		struuid = generate().c_str();
		return struuid.c_str();
	}
}

PLUGIN_API int sendMsg(PHandle pHandle, const char * json)
{
	int code = 0;
	bridgeHandle* p_bridge= (bridgeHandle*)pHandle;
	p_bridge->send(json);
	return code;
}

PLUGIN_API int regReceiveCallback(PHandle pHandle, std::function<void(char*)>cb)
{
	bridgeHandle* p_bridge = (bridgeHandle*)pHandle;
	p_bridge->regReceiveCallback(cb);
	return  0;
}

PLUGIN_API int UnInitBridge(PHandle* pHandle)
{
	if (*pHandle != NULL) {
		bridgeHandle* pCurrentHandle =  (bridgeHandle*) (*pHandle);
		pCurrentHandle->Uninit();
		delete pCurrentHandle;
	}
	*pHandle = NULL;
	return true;
}
